from flask import Flask, render_template, redirect, url_for
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key'

login_manager = LoginManager(app)
login_manager.login_view = 'login'

# 模拟用户类
class User(UserMixin):
    def __init__(self, user_id):
        self.id = user_id

# 模拟用户数据库
users = {1: {'id': 1, 'username': 'john_doe', 'password': 'password'}}

@login_manager.user_loader
def load_user(user_id):
    return User(user_id)

@app.route('/')
def index():
    return redirect(url_for('login'))

@app.route('/login')
def login():
    user = User(1)
    login_user(user)
    return redirect(url_for('dashboard'))

@app.route('/dashboard')
@login_required
def dashboard():
    return f'Hello, {current_user.id}!'

@app.route('/logout')
@login_required
def logout():
    logout_user()
    return 'Logged out successfully!'

if __name__ == '__main__':
    app.run(debug=True)
